$(function () {
    init_data();
});
/**加载客户详情**/
function init_data() {
    var bpId = $("#customer_ids").val();
    if (bpId == undefined){$JuXue.n_warning("获取得用户资料失败");return;}
    $.post(ctx+"bigEpResoure/viewBigEmpDetail",{keyid:bpId},function (r) {
        if (r.code === 0){
            var data = r.msg;
            $("#reciver").text(data.bpDistribute);
            $("#cus_batch").text(data.bpBatch);
            $("#cus_batch_td").text(data.bpBatch);$("#exName").text(data.bpName);
            $("#cus_name").text(data.bpName);$("#cus_sex").text(data.bpSex);$("#cus_pnoe").text(data.bpMobile);
            $("#cusphone").text(data.bpMobile);$("#cusEmail").text(data.bpEmail);$("#cus_email").text(data.bpEmail);
            $("#cus_wechat").text(data.bpWechat);$("#cus_qq").text(data.bpQq);$("#cus_director").text(data.bpDirector);
            $("#cus_agent").text(data.bpAgent);$("#cus_bpAgentProxy").text(data.bpAgentProxy);
            $("#cus_chanel").text(data.bpSourChanel);$("#cus_project").text(data.bpInteProject);
            $("#cus_area").text(data.bpCity);$("#cus_zone").text(data.bpZone);$("#cus_collage").text(data.bpColleges);
            $("#cus_acamy").text(data.bpAcademy);$("#cus_major").text(data.bpMajor);$("#cus_year").text(data.bpYear);
            var education = data.bpEducation;
            var bpEducation = "";
            if(education=='2'){bpEducation = "专科"}else if(education=='3'){bpEducation = "本科";}else if(education=='4'){bpEducation = "硕士";}
            else if(education=='5'){bpEducation = "博士";}else {bpEducation = "未知";}
            $("#bp_education").text(bpEducation);

            if (data.bpGroudBatch!=null){ $("#cus_groud_batch").text(data.bpGroudBatch);}else{
                $("#cus_groud_batch").text("空");
            }
            //分配关系
            var cus = data.bpMyCustomerName;
            if (cus != null){
                var cusArray = cus.split(",");
                $("#my_customesharea").empty();
                $.each(cusArray,function (index,val) {
                    $("#my_customesharea").append("<button type=\"button\" class=\"btn btn-success m-r-5 m-b-5\">"+val+"</button>");
                });
            }
            /**初始化用户修改得事件**/
            init_table_Event(data.bpId);

            var stage = data.bpStage;
            init_step(stage);

            init_customer_label();
            /**初始化销售计划**/
            init_SalePlan(data.bpUuid,"");$("#selectUserUuid").val(data.bpUuid);
        }else{
            $JuXue.n_danger(r.msg);
        }
    });

    /**初始化标签得内容**/
    $.post(ctx + "saleConfig/selectSaletag", {}, function (r) {
        if (r.code == 0) {
            var data = r.msg;
            $.each(data, function (index, val) {
                var pid = val.parent.tagUuid;
                var div =  $(' <div class="text-left border-bottom-1" pid="'+pid+'" typeid="1"></div>');
                var fistdiv  ='<div class="border-bottom-1 p-t-3"><div class="checkbox m-0 "><span class="m-r-15 m-l-5">'+val.parent.tagGroupName+'</span>' +
                    '<select class="width-200 form-control  inline-block m-b-3" onchange="selectLabelTypeupdate(this)" ><option value="1">保持本组原有得标签</option>' +
                    '<option value="2">不添加本组标签,删除原有标签</option><option value="3">使用新的标签替换本组标签</option></select>' +
                    '<label><input type="hidden" value='+val.parent.tagSelect+' /></label></div></div>';
                div.append(fistdiv);
                var cc = $("<div class='m-t-5 p-3 text-left'><input type='hidden' value="+val.parent.tagSelect+" /></div>");
                $.each(val.children,function (i,v) {
                    cc.append('<button type="button" disabled onclick="selectTagLabelupdate(this)" data-id="'+v.tagUuids+'" ' +
                        'class="btn btn-xs btn-link width-100 m-l-5 p-2 m-b-5 f-s-12 border-solid-g">'+v.tagName+'</button>');
                });
                div.append(cc);
                $("#selectTagupdate").append(div);
            });
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
    loadDetailList();
}

function init_step(stage) {
    /**初始化客户阶段**/
    $.post(ctx + "saleConfig/initTrackTable", function (r) {
        var data = r.data;
        if (data != null) {
            var oneStage = data[0].saleName;
            var twoStage = data[1].saleName;
            var threeStage = data[2].saleName;
            var fourStage = data[3].saleName;
            var fiveStage = data[4].saleName;
            $('.step-box').step({
                stepDirection:'x',
                showStepButton:false,
                stepCount:5,
                stepTitles:[oneStage,twoStage,threeStage,fourStage,fiveStage],
            });
            $(".eis-form-steps").find(".eis-step-icon").eq(stage-1).trigger('click');
            if(stage==1){
                $(".eis-step-progress").css("width","10%");
            }else if(stage==2){
                $(".eis-step-progress").css("width","30%");
            }else if(stage==3){
                $(".eis-step-progress").css("width","50%");
            }else if(stage==4){
                $(".eis-step-progress").css("width","70%");
            }else if(stage==5){
                $(".eis-step-progress").css("width","80%");
            }
        } else {
            $JuXue.n_danger("初始化阶段失败");
        }
    });
}

/**初始化大资源的销售计划**/
function init_SalePlan(uuid,isImpl) {
    $.post(ctx+"SaleHelp/selectScalePlanByUserUUid",{userUuid:uuid,isImpl:isImpl},function (r) {
        if (r.code == 0){
            var data = r.msg;
            $("#sale_list_mycustomer").empty();
            $.each(data,function (index,val) {
                var type = val.cPlanType;
                var lanel = "";
                if (type == "1"){
                    lanel = "<label><i class=\"fa fa-star text-success\"></i></label> ";
                }else{
                    lanel = "<label><i class=\"fa fa-phone text-danger\"></i></label> ";
                }
                var li= "<li class=\"inverse p-10\">\n" +
                    "    <div class=\"email-checkbox\">"+lanel+" </div>\n" +
                    "      <div class=\"email-info\">\n" +
                    "         <span class=\"email-time\">"+val.cPlanTime+"</span>\n" +
                    "         <h5 class=\"email-title\">\n" +
                    "            <span href=\"#\"  data-toggle=\"ajax\">"+val.cPlanTitle+"</span>\n" +
                    "         </h5>\n" +
                    "          <p class=\"email-desc m-0\">"+val.cPlanDesc+"</p>\n" +
                    "          <div class=\"btn-group dropdown pull-right\">\n" +
                    "              <a href=\"javascript:;\" onclick='deleteSalePlanByUserUuid(this)' uuid='"+val.cPlanId+"' userUuid='"+uuid+"' ><i class=\"fa fa-times text-danger f-s-10 fa-fw m-r-5\"></i>删除</a>\n" +
                    "          </div>\n" +
                    "      <div class=\"clearfix\"></div>\n" +
                    "     </div>\n" +
                    " </li>"
                $("#sale_list_mycustomer").append(li).append("<hr class='m-0'/>");
            });
        }else{
            $JuXue.n_danger("初始化客户的销售计划失败");
        }
    });
}
/**切换计划未执行还是已经执行**/
function salePlanIsImple(btn) {
    $("#selectLabel").find("button").each(function () {
        $(this).removeClass("btn-primary").addClass("btn-link");
    });
    $(btn).removeClass("btn-link").addClass("btn-primary");
    var uuid = $("#selectUserUuid").val();
    var isImpl = $(btn).attr("uuid");
    init_SalePlan(uuid,isImpl);
}
/**删除销售计划**/
function deleteSalePlanByUserUuid(btn) {
    var  uuid = $(btn).attr("uuid");
    var userid = $(btn).attr("userUuid");
    $JuXue.confirm({
        text: "确定删除销售计划？",
        confirmButtonText: "确定删除"
    }, function() {
        $.post(ctx+"SaleHelp/deleteScalePlanByUseridUuid",{palnId:uuid,userUuid:userid,types:"3"},function (r) {
            if (r.code == 0){
                $JuXue.n_success("删除成功");
                $(btn).parent().parent().parent().remove();
            }else{
                $JuXue.n_danger("删除失败");
            }
        });
    });
}
/**初始化客户的标签**/
function init_customer_label() {
    var ex_id = $("#customer_ids").val();
    if (ex_id == "" || ex_id == undefined) {$JuXue.n_danger("初始化客户标签失败");return}
    $.post(ctx + "bigEpResoure/getBigEmpDetailLabel", {keyid: ex_id}, function (r) {
        if(r.code == 0){
            var data = r.msg;
            if (data!=null){
                var bpLabelName  = data.bpLabelName;
                $("#drownSelectupdate").empty();
                if (bpLabelName!=null){
                    var name = bpLabelName.split(",");
                    $.each(name,function (i,v) {
                        $("#drownSelectupdate").append('<a type="button" class="btn btn-link width-75 m-5 p-3 f-s-10 btn-primary ">'+v+'</a>');
                    });
                }
            }
        }
    });
}
/**选择标签**/
function selectTagLabelupdate(btn) {
    var $parentcheck  = $(btn).parent();
    var isMultiInput = $parentcheck.children("input:first-child").val();
    if (isMultiInput == "1"){
        //多选
        var ishave = $(btn).hasClass("btn-primary");
        if (ishave) {
            $(btn).removeClass("btn-primary");
        }else{
            $(btn).addClass("btn-primary");
        }
    }else{
        //单选
        var ishave = $(btn).hasClass("btn-primary");
        if (ishave) {
            $parentcheck.find("button").each(function () {
                $(this).removeClass("btn-primary");
            });
        }else{
            $parentcheck.find("button").each(function () {
                $(this).removeClass("btn-primary");
            });
            $(btn).addClass("btn-primary");
        }
    }
}
/**整体追加标签保存**/
function labelSelectSureupdate() {
    var div = $("#selectTagupdate").children("div");
    var selectids = new Array();
    $.each(div,function (index,val) {
        var btn = $(this).find("button");
        $.each(btn,function () {
            var isSelect = $(this).hasClass("btn-primary");
            if (isSelect){
                var selectid = $(this).attr("data-id");
                selectids.push(selectid);
            }
        });
    });
    var length = selectids.length;
    if(length > 10){$JuXue.n_danger("标签选择不能大于10个");return ;}

    /**选择类型**/
    var selectTypepid = new Array();
    /**typeid**/
    var selectTypeid = new Array();
    var labeldiv = $("#selectTagupdate").children("div");
    $.each(labeldiv,function () {
        var typeids= $(this).attr("typeid");
        selectTypeid.push(typeids);
        var pid= $(this).attr("pid");
        selectTypepid.push(pid);
    });
    /**获取资源类型**/
    var sourceType = $("#customer_type").val();
    //获取标签
    var label = selectids.join(",");
    /**获取修改的ids*/
    var ex_id = $("#customer_ids").val();
    /**类型**/
    var pidstr = selectTypepid.join(",");
    var idstr = selectTypeid.join(",");

    $.post(ctx+"myCustomer/setLabelMyCustomer",{"uuid":label,ids:ex_id,"types":sourceType,label:label,pid:pidstr,idstr:idstr},function (r) {
        if (r.code == 0){
            $JuXue.n_success("设置成功");
            $JuXue.refreshTable('myEmpTable');
            init_customer_label();
            $('#labeldetail').removeClass('open');
        }else{
            $JuXue.n_danger("设置失败");
        }
    });
}
/**切换选择类型**/
function selectLabelTypeupdate(btn) {
    var option = $(btn).val();
    if (option == "1" ||option =="2"){
        var allbutton = $(btn).parent().parent().next();
        allbutton.find("button").each(function () {
            $(this).attr("disabled","disabled");
            $(this).removeClass("btn-primary");
        });
    }else {
        var allbutton = $(btn).parent().parent().next();
        allbutton.find("button").each(function () {
            $(this).removeAttrs("disabled");
        });
    }
    $(btn).parent().parent().parent().attr("typeid",option);
}
/**切换客户阶段**/
function changeStage(btn) {
    var stage = $(btn).attr("stage");
    //获取当前更新得字段
    var ex_id = $("#customer_ids").val();
    $.post(ctx+"bigEpResoure/bigempEditinfo",{bpId:ex_id,bpStage:stage},function (r) {
        if (r.code == 0){
            if(stage==1){
                $(".eis-step-progress").css("width","10%");
            }else if(stage==2){
                $(".eis-step-progress").css("width","30%");
            }else if(stage==3){
                $(".eis-step-progress").css("width","50%");
            }else if(stage==4){
                $(".eis-step-progress").css("width","70%");
            }else if(stage==5){
                $(".eis-step-progress").css("width","80%");
            }
            $JuXue.n_success("更新成功");
            loadDetailList();//刷新 动态记录
        }else{
            $JuXue.n_danger("更新失败");
        }
    });
}
/**初始化修改用户得一些修改事件**/
function init_table_Event(bpId) {
    /**修改客户名称**/
    $("#cus_name").editable({
        url:function (parame) {
            $.post(ctx+"bigEpResoure/bigempEditinfo",{bpName:parame.value,bpId:bpId},function (r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                    $("#cusName").text(parame.value);
                    $JuXue.refreshTable("bigEmpTable");
                } else {
                    $JuXue.n_danger(r.msg);
                }
            });
        },
        pk:bpId,
        defaultValue:"",
        title:"修改客户名称",
        validate: function (value) { //字段验证
            if (!$.trim(value)) {
                return '不能为空';
            }
        }
    });
    /**修改学历层次**/
    $("#bp_education").editable({
        type: "select",
        url:function (parame) {
            $.post(ctx+"bigEpResoure/bigempEditinfo",{bpEducation:parame.value,bpId:bpId},function (r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                    $("#bp_education").text(parame.value);
                    $JuXue.refreshTable("bigEmpTable");
                } else {
                    $JuXue.n_danger(r.msg);
                }
            });
        },
        pk:bpId,
        defaultValue:"",
        source: [{ value: "专科", text: "专科" }, { value: "本科", text: "本科" }, {value:"硕士",text:"硕士"},
            { value: "博士", text: "博士" }
        ],
        title: "修改学历层次",
        disabled: false,
        emptytext: "",
        mode: "popup",
        validate: function (value) { //字段验证
            if (!$.trim(value)) {
                return '不能为空';
            }
        }
    });
    /**修改手机号**/
    $("#cus_pnoe").editable({
        url:function (parame) {
            $.post(ctx+"bigEpResoure/bigempEditinfo",{bpMobile:parame.value,bpId:bpId},function (r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                    $("#cusphone").text(parame.value);
                } else {
                    $JuXue.n_danger(r.msg);
                }
            });
        },
        pk:bpId,
        defaultValue:"",
        title:"修改手机号",
        validate: function (value) { //字段验证
            if (!$.trim(value)) {
                return '不能为空';
            }
        }
    });
    /**修改微信**/
    $("#cus_wechat").editable({
        url:function (parame) {
            $.post(ctx+"bigEpResoure/bigempEditinfo",{bpWechat:parame.value,bpId:bpId},function (r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                } else {
                    $JuXue.n_danger(r.msg);
                }
            });
        },
        pk:bpId,
        defaultValue:"",
        title:"修改微信",
        validate: function (value) { //字段验证
            if (!$.trim(value)) {
                return '不能为空';
            }
        }
    });
    /**修改QQ**/
    $("#cus_qq").editable({
        url:function (parame) {
            $.post(ctx+"bigEpResoure/bigempEditinfo",{bpQq:parame.value,bpId:bpId},function (r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                } else {
                    $JuXue.n_danger(r.msg);
                }
            });
        },
        pk:bpId,
        defaultValue:"",
        title:"修改QQ",
        validate: function (value) { //字段验证
            if (!$.trim(value)) {
                return '不能为空';
            }
        }
    });
    /**修改邮箱**/
    $("#cus_email").editable({
        url:function (parame) {
            $.post(ctx+"bigEpResoure/bigempEditinfo",{bpEmail:parame.value,bpId:bpId},function (r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                    $("#cusEmail").text(parame.value);
                } else {
                    $JuXue.n_danger(r.msg);
                }
            });
        },
        pk:bpId,
        defaultValue:"",
        title:"修改邮箱",
        validate: function (value) { //字段验证
            if (!$.trim(value)) {
                return '不能为空';
            }
        }
    });
    /**修改意向等级**/
    $("#cus_groud_batch").editable({
        type: "select",
        url:function (parame) {
            $.post(ctx+"bigEpResoure/bigempEditinfo",{bpGroudBatch:parame.value,bpId:bpId},function (r) {
                if (r.code === 0) {
                    $JuXue.n_success(r.msg);
                    $("#cus_groud_batch").text(parame.value);
                } else {
                    $JuXue.n_danger(r.msg);
                }
            });
        },
        pk:bpId,
        defaultValue:"",
        source: [{ value: "A", text: "A" }, { value: "B", text: "B" }, {value:"C",text:"C"},
            { value: "D", text: "D" }, { value: "E", text: "E" }, {value:"F",text:"F"}
        ],
        title: "选择意向等级",
        disabled: false,
        emptytext: "",
        mode: "popup",
        validate: function (value) { //字段验证
            if (!$.trim(value)) {
                return '不能为空';
            }
        }
    });
}
/**添加客户跟进*/
function addFollow() {
    var uuid = $("#customer_ids").val();
    var content = $("#followContent").val();
    var type = "3";
    var url = ctx+"myCustomer/addFollow";
    $.post(url,{dyCusUuid:uuid,dyFollowContent:content,dySourceType:type},function (r) {
        if(r.code===0){
            $("#followContent").val('');
            $JuXue.n_success(r.msg);
            loadDetailList();
        }else{
            $JuXue.n_danger(r.msg);
        }
    });
}

function loadDetailList(){
    var uuid = $("#customer_ids").val();
    $("#detailListId").empty();
    /**加载客户的动态*/
    $.post(ctx+"myCustomer/getDetailFollow",{dyCusUuid:uuid,dySourceType:"3"},function (r) {
        if(r.code===0){
            var data = r.data;
            $("#detailListId").empty().append(data);
            $("#detailListId").append(
                '<div class="col-md-12 col-sm-12 col-xs-12 p-t-15">' +
                '    <div class="text-center">' +
                '       <small><strong>已全部加载完成</strong></small>' +
                '    </div>' +
                '</div>');
        }
    });
}

